package com.xx.sg;

import com.xx.sg.tcp.server.TCPServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;

import java.net.InetSocketAddress;

@EnableAsync
@EnableScheduling
@SpringBootApplication
public class SgApplication implements CommandLineRunner {

    @Value("${tcp.server.host}")
    private String host_server;

    @Value("${tcp.server.port}")
    private Integer port_server;


    @Autowired
    private TCPServer tcpServer;

    public static void main(String[] args) {
        SpringApplication.run(SgApplication.class, args);
    }

    @Async
    @Override
    public void run(String... args) {
        Thread tcpServerThread = new Thread(() -> {
            InetSocketAddress address = new InetSocketAddress(port_server);
            System.out.println("Tcp服务器启动: " + host_server + ":" + port_server);
            tcpServer.start(address);
        });
        tcpServerThread.start();
    }
}
